package com.artron.mediaartron.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.artron.baselib.base.BaseFragment;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.fragment.login.LoginFragment;
import com.artron.mediaartron.ui.fragment.login.RegisterFragment;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-11.
 */

public class LoginActivity extends AppBaseActivity {

    private static final String TYPE = "Type";

    @BindView(R.id.General_iv_bg)
    protected ImageView mIvBg;

    public static final int TYPE_LOGIN = 6000;
    public static final int TYPE_REGISTER = 6001;
    public static final int TYPE_RESET = 6002;
    public static final int TYPE_BIND_PHONE = 6003;

    public static void start(Context context, int type) {
        Intent starter = new Intent(context, LoginActivity.class);
        starter.putExtra(TYPE, type);
        context.startActivity(starter);
    }

    public static void startForResult(Context context, int type, int requestCode) {
        Intent starter = new Intent(context, LoginActivity.class);
        starter.putExtra(TYPE, type);
        ((Activity) context).startActivityForResult(starter, requestCode);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        int type = getIntent().getIntExtra(TYPE, TYPE_LOGIN);
        BaseFragment fragment;
        if (type == TYPE_LOGIN) {
            fragment = LoginFragment.newInstance();
            mTvTitle.setText("登录");
        } else if (type == TYPE_REGISTER) {
            fragment = RegisterFragment.newInstance(type);
            mTvTitle.setText("注册");
        } else if (type == TYPE_BIND_PHONE) {
            fragment = RegisterFragment.newInstance(type);
            mTvTitle.setText("绑定手机号");
        } else {
            fragment = RegisterFragment.newInstance(type);
            mTvTitle.setText("重置密码");
        }

        addFragment(R.id.General_content, fragment, false);

    }

    @Override
    protected void initActionBar() {
        super.initActionBar();

        mToolbar.setBackgroundColor(UIUtils.getColor(R.color.transparent));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            设置状态栏为透明
            getWindow().setStatusBarColor(Color.TRANSPARENT);
//            设置window的状态栏不可见
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
//        view不根据系统窗口来调整自己的布局
        ViewGroup mContentView = (ViewGroup) getWindow().findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            mChildView.setFitsSystemWindows(false);
            ViewCompat.requestApplyInsets(mChildView);
        }
        int statusBarHeight = UIUtils.getStatusBarHeight();
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mToolbar.getLayoutParams();
        params.topMargin = statusBarHeight;
        mToolbar.setLayoutParams(params);

        Drawable tint = UIUtils.tint(R.drawable.ic_back, R.color.white);
        mToolbar.setNavigationIcon(tint);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.menu_register);
        int type = getIntent().getIntExtra(TYPE, TYPE_LOGIN);
        if (type == TYPE_LOGIN) {
            item.setTitle("注册");
        } else if (type == TYPE_REGISTER) {
            item.setTitle("登录");
        } else if (type == TYPE_BIND_PHONE) {
            item.setTitle("跳过");
        } else {
            item.setVisible(false);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
